<template>
	<view class="infoBox_peripherals" style="background-color: white;padding-top: 30px;">
		<view class="infoBox_peripherals_title" style="height: 32px;margin: 15px 0 5px 0;">
			<view class="infoBox_peripherals_title__left" style="height: 100%;">
				<view class="infoBox_peripherals_title__left_bgbox"></view>
				<input style="border: 1px solid #dfdcdc;height: 100%;border-radius: 20px;padding: 4px 7px;"
					v-model="ckfill" @input="getsuggest" placeholder="输入要搜索的位置" @confirm="confirmInput">
			</view>
		</view>
		<view class="" v-if="suggestion.length ===0">
			<view class="infoBox_peripherals_mapbox">
				<map style="width: 100%;" class="infoBox_peripherals_mapbox__map" id="map" :latitude="mapxx.latitude"
					:longitude="mapxx.longitude" :scale="mapxx.scale" :markers="mapxx.markers"></map>
			</view>

			<view class="infoBox_peripherals_tabs">
				<u-tabs :list="list" :current="tabsCurrent" @change="tabsChange"></u-tabs>
			</view>
			<view class="infoBox_peripherals_tabsitem">
				<view style="border-bottom: 1px solid #ccc;" v-for="(item,index) in peripheralsData" :key="index"
					class="infoBox_peripherals_tabsitem_items">
					<view class="infoBox_peripherals_tabsitem_items_left">
						<image src="../../static/index/location-icon1@2x.png" style="width: 26rpx;height: 34rpx;">
						</image>
						<view class="infoBox_peripherals_tabsitem_items_left_text">{{item.title}}</view>
					</view>
					<view class="infoBox_peripherals_tabsitem_items_right">{{item.distance}}m</view>
				</view>
			</view>

		</view>

		<!--关键词输入提示列表渲染-->
		<view class="" v-else>
			<view v-for="(item,index) in suggestion" :key="index" style="border-bottom: 1px solid #ccc;margin: 4px;">
				<!--绑定回填事件-->
				<view>
					<!--根据需求渲染相应数据-->
					<!--渲染地址title-->
					<view style="text-align:center;" @click="backfill(item.id)">{{item.title}}</view>
					<!--渲染详细地址-->
					<view style=" font-size:12px;color:#666;text-align:center;">{{item.addr}}</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import QQMapWx from '@/common/qqmap-wx-jssdk.min.js'
	let qqmapsdk;
	qqmapsdk = new QQMapWx({
		key: '7ABBZ-XEH6W-SPNRN-YCPQ2-I3QOH-R7BXF' // 在腾讯地图上申请的key
	});
	let infowidth = 32,
		infoheight = 42;
	let infoiconPath = '../../static/mapview/loaction-red.png';

	export default {
		data() {
			return {
				/** 地图定位  **/
				tabsCurrent: 0,
				list: [{
						name: '交通'
					},
					{
						name: '学校'
					},
					{
						name: '医疗'
					},
					{
						name: '生活'
					},
					{
						name: '休闲'
					}
				],
				peripheralsData: [],
				// 地图相关
				mapxx: {
					latitude: 35.931616,
					longitude: 120.008822,
					scale: 16,
					markers: {
						id: 0,
						latitude: 35.931616,
						longitude: 120.008822,
						iconPath: infoiconPath,
					}
				},
				/** 关键词检索  **/
				suggestion: [],
				ckfill: ''

			}

		},
		onLoad(data) {
			this.initmap();
		},
		filters: {
			filtercou(item) {
				if (!item) {
					return '暂未采集';
				} else {
					return item;
				}
			}
		},
		methods: {
			// 地图相关
			// 周边查询，切换tabs
			tabsChange(index) {
				this.tabsCurrent = index
				let tabName = ''
				if (index === 0) {
					tabName = '交通'
				} else if (index === 1) {
					tabName = '学校'
				} else if (index === 2) {
					tabName = '医疗'
				} else if (index === 3) {
					tabName = '生活'
				} else if (index === 4) {
					tabName = '休闲'
				}
				this.searchNearby(tabName);
			},
			searchNearby(name) {
				let that = this;

				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						var latitude = res.latitude; // 纬度
						var longitude = res.longitude; // 经度
						console.log('经度：' + longitude + '，纬度：' + latitude);
					}
				});

				uni.showToast({
					title: '请稍后',
					duration: 2000
				})


				qqmapsdk.search({
					keyword: name,
					page_size: 5,
					location: this.mapxx.latitude + ',' + this.mapxx.longitude,
					success: (res) => { //搜索成功后的回调
						uni.hideToast({})
						let arrlist = [];
						for (var i = 0; i < res.data.length; i++) {
							arrlist.push({ // 获取返回结果，放到mks数组中
								title: res.data[i].title,
								latitude: res.data[i].location.lat,
								longitude: res.data[i].location.lng,
								distance: res.data[i]._distance,
							})
						}
						// 每次不用重新赋值，通过下标给需要的赋值
						this.peripheralsData = arrlist;
					},
					fail: function(res) {
						console.log(res);
					},
					complete: function(res) {}
				});
			},
			initmap() {
				//获取当前的地理位置
				let vthis = this;
				uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						this.mapxx.latitude = res.latitude;
						this.mapxx.longitude = res.longitude;
						this.mapxx.markers = [{
							id: 1,
							latitude: res.latitude,
							longitude: res.longitude,
							iconPath: infoiconPath
						}];
						console.log('当前位置的经度：' + res.longitude);
						console.log('当前位置的纬度：' + res.latitude);
						this.searchNearby('交通');
					}
				});
			},

			/** 关键词输入 **/
			backfill(id) {
				const that = this
				this.suggestion.forEach(item => {
					if (item.id == id) {
						that.ckfill = item.title
					}
				})
				this.confirmInput()
			},
			//触发关键词输入提示事件
			getsuggest(e) {
				const _this = this;
				qqmapsdk.getSuggestion({
					keyword: e.detail.value,
					success: function(res) {
						console.log(res);
						var sug = [];
						for (var i = 0; i < res.data.length; i++) {
							sug.push({
								title: res.data[i].title,
								id: res.data[i].id,
								addr: res.data[i].address,
								city: res.data[i].city,
								district: res.data[i].district,
								latitude: res.data[i].location.lat,
								longitude: res.data[i].location.lng
							});
						}
						_this.suggestion = sug; // 使用 _this 替代 this
					},
					fail: function(error) {
						console.error(error);
					},
					complete: function(res) {
						console.log(res);
					}
				});
			},
			confirmInput() {
				uni.navigateTo({
					url: '../../pages/publish/publish?address='+ encodeURIComponent(this.ckfill)
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	// 周边设备
	#{if(&, "&", "*")}_peripherals {
		background: #FFFFFF;
		box-shadow: 0rpx 4rpx 20rpx 0rpx rgba(0, 0, 0, 0.05);
		border-radius: 16rpx;
		margin-bottom: 80rpx;

		#{if(&, "&", "*")}_title {
			display: flex;
			justify-content: space-between;
			padding: 14px 12px;
			font-size: 14px;
			letter-spacing: 1px;

			#{if(&, "&", "*")}__left {
				display: flex;
				font-size: 24rpx;
				font-weight: 600;
				color: #00A39C;

				#{if(&, "&", "*")}_bgbox {
					width: 6rpx;
					height: 28rpx;
					background: #00A39C;
					border-radius: 3rpx;
					margin-right: 12rpx;
				}
			}

			#{if(&, "&", "*")}__right {
				font-weight: 600;

				#{if(&, "&", "*")}__green {
					color: #00AF99;
				}

				#{if(&, "&", "*")}__yellow {
					color: #FBAD00;
				}
			}
		}

		#{if(&, "&", "*")}_mapbox {
			width: 100%;
			height: 400rpx;
			border-radius: 12rpx;
			padding: 12px 14px;
			box-sizing: border-box;

			display: flex;
			justify-content: center;
			align-items: center;
			background-color: #fff;

			#{if(&, "&", "*")}__map {
				height: 398rpx;
				width: 100%;
				border-radius: 5px;
				background-color: #fff;
			}
		}

		#{if(&, "&", "*")}_tabs {
			// padding: 12px 14px;
		}

		#{if(&, "&", "*")}_tabsitem {
			padding: 14px 12px;

			#{if(&, "&", "*")}_items {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 18rpx;

				#{if(&, "&", "*")}_left {
					display: flex;
					align-items: center;
					font-size: 28rpx;
					font-weight: 400;
					color: #333333;

					#{if(&, "&", "*")}_text {
						margin-left: 10rpx;
					}
				}

				#{if(&, "&", "*")}_right {
					font-size: 24rpx;
					font-weight: 400;
					color: #999999;
				}
			}
		}
	}
</style>